<template>
  <el-dialog
    v-model="visible"
    title="企业微信AI坐席绑定"
    width="80%"
    top="5vh"
    :lock-scroll="true"
    :append-to-body="true"
    :destroy-on-close="true"
  >
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="68px"
      >
        <el-form-item label="用户昵称" prop="nickname">
          <el-input
            v-model="queryParams.nickname"
            class="!w-240px"
            clearable
            placeholder="请输入用户昵称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="queryParams.mobile"
            class="!w-240px"
            clearable
            placeholder="请输入手机号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon class="mr-5px" icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon class="mr-5px" icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        ref="tableRef"
        v-loading="loading"
        :data="list"
        :show-overflow-tooltip="true"
        :row-key="(row) => row.id"
        :highlight-current-row="true"
        @current-change="handleCurrentChange"
        @row-click="handleRowClick"
      >
        <el-table-column align="center" label="用户编号" prop="id" width="120px" />
        <el-table-column align="center" label="头像" prop="avatar" width="80px">
          <template #default="scope">
            <img v-if="scope.row.avatar" :src="scope.row.avatar" style="width: 40px" />
            <div v-else style="width: 40px; height: 40px; background-color: #f0f0f0;margin: 0 auto;"></div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="手机号" prop="mobile" width="180px" />
        <el-table-column align="center" label="昵称" prop="nickname" width="180px" />
        <el-table-column align="center" label="等级" prop="levelName" width="100px" />
        <el-table-column align="center" label="分组" prop="groupName" width="100px" />
        <el-table-column
          :show-overflow-tooltip="false"
          align="center"
          label="用户标签"
          prop="tagNames"
        >
          <template #default="scope">
            <el-tag v-for="(tagName, index) in scope.row.tagNames" :key="index" class="mr-5px">
              {{ tagName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="积分" prop="point" width="100px" />
        <el-table-column align="center" label="状态" prop="status" width="100px">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column
          :formatter="dateFormatter"
          align="center"
          label="登录时间"
          prop="loginDate"
          width="180px"
        />
        <el-table-column
          :formatter="dateFormatter"
          align="center"
          label="注册时间"
          prop="createTime"
          width="180px"
        />
      </el-table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNo"
        :total="total"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="close">关闭</el-button>
        <el-button type="primary" @click="submitForm">绑定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useMessage } from '@/hooks/web/useMessage'
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { bindUser } from './api'
import { getUserPage, UserVO } from '@/api/member/user'

const emit = defineEmits(['close', 'save'])
const message = useMessage()
const visible = ref(false)
const loading = ref(false)
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const id = ref('')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: null,
  mobile: null
})
const queryFormRef = ref() // 搜索的表单
const selected = ref<UserVO | null>(null) // 表格的选中用户对象
const tableRef = ref()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 当前行选中事件 */
const handleCurrentChange = (row: UserVO) => {
  selected.value = row
}

/** 行点击事件 */
const handleRowClick = (row: UserVO) => {
  selected.value = row
}

const open = (_id: string) => {
  visible.value = true
  id.value = _id
  getList()
  selected.value = null
}

const close = () => {
  visible.value = false
  emit('close')
}

const submitForm = () => {
  if (!selected.value) {
    message.error('请选择用户')
    return
  }
  bindUser({
    id: id.value,
    userId: selected.value.id,
    ownerName: selected.value.nickname
  })
    .then(() => {
      loading.value = false
      message.success('保存成功')
      emit('save')
      visible.value = false
    })
    .catch(() => {
      loading.value = false
    })
}

defineExpose({
  open,
  close
})
</script>
